package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;

import com.ab.soap.AbSoapListener;
import com.ab.soap.AbSoapParams;
import com.ab.task.AbTask;
import com.ab.task.AbTaskItem;
import com.ab.task.AbTaskObjectListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.SpLogView;
import com.htandroid.htandroid.sys.Basenvo;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.base_index;
import com.htandroid.htandroid.webservice.abXMLSploglist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

/**
 * Created by lkp on 2015/11/29.
 */
public class Basenvo_splogview extends base_index {
    String tablename, cvouchid, ztid;//
    ArrayList<SpLogView> sploglist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.basenvo_splogview);
        initview("日志查看");
        initview2();
        getdata(0);
    }

    //初始化2
    @Override
    public void initview2() {
        //获取传入的数据
        Intent intent = getIntent();// 创建Intent对象
        Bundle bundle = intent.getExtras();// 获取传入的数据，并使用Bundle记录
        tablename = bundle.getString("tablename");// 获取Bundle中记录的信息
        cvouchid = bundle.getString("cvouchid");// 获取Bundle中记录的信息
        ztid = bundle.getString("ztid");
    }

    //获取数据
    @Override
    public void getdata(int page) {
//        TreeMap<String, String> properties = new TreeMap<String, String>();
//        //参数
//        String[] parms = {"cacc_id", "iyear", "tablename", "cvouchid"};
//        String[] vals = {(String) myApplication.getGs_ztid(), (String) myApplication.getGs_year(), tablename, cvouchid};
//        //生成xml
//        BuildQgdListXMLService service = new BuildQgdListXMLService();
//        String xmlstring = service.buildXML(null, "splog_view", InstorageURL.NAME_SPACE, parms, vals, null);
//        adatperDataList.clear();
//        ToolSOAPxml.callService("splog_view", xmlstring, new ToolSOAPxml.WebServiceCallBack() {
//            @Override
//            public void onSucced(String result) {
//                adatperDataList.clear();
//                ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
//                if (result != null) {
//                    //解析xml文件,返回列表
//                    sploglist = XMLSploglist.paraseCommentInfors(result);
//
//                    for (int i = 0; i < sploglist.size(); i++) {
//                        SpLogView splog = new SpLogView();
//                        splog = sploglist.get(i);
//                        //                            aaa.add(item.getPropertyAsString(0));
//                        Map<String, Object> map = new HashMap<String, Object>();
//                        ToolLog.log("data:" + splog.getCdate());
//                        map.put("index", (i + 1));
//                        map.put("cvouchid", "单据编号：" + splog.getCvouchid());
//                        map.put("cdept", "操作时间：" + Basenvo.customDateFormat("yyyy-MM-dd HH:mm:ss", splog.getCdate()));
//                        map.put("cnocheckreason",
//                                splog.getCmemo());
//                        map.put("ddate",
//                                "");
//                        //更多
//                        map.put("data5",
//                                "" + splog.getCmemo());
//                        map.put("data6",
//                                "");
//                        map.put("data7",
//                                "");
//                        map.put("data8",
//                                "");
//                        dataListForInit.add(map);
//                    }
//                } else {
//                    AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                }
//                toolDataProvider.initData(dataListForInit);
//                loadList();
//                //mHandler.sendEmptyMessage(LIST_GET_FIRST_PAGE); //加载完毕发送指令显示第一页
//                //swipeRefreshLayout.setRefreshing(false);
//            }
//
//            @Override
//            public void onFailure(String result) {
//                AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                //swipeRefreshLayout.setRefreshing(false);
//                //ToolLog.log("err：" + result);
//            }
//        });

        AbTask task = new AbTask();
        final AbTaskItem item = new AbTaskItem();
        item.setListener(new AbTaskObjectListener() {

            @Override
            public <T> void update(T entity) {
                String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
                AbSoapParams params = new AbSoapParams();
                params.put("arg0", ztid);
                params.put("arg1", myApplication.getGs_year());
                params.put("arg2", tablename);
                params.put("arg3", cvouchid);
                mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "splog_view", params, new AbSoapListener() {
                    //获取数据成功会调用这里
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        adatperDataList.clear();
                        ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
                        if (content != null) {
                            sploglist = abXMLSploglist.paraseCommentInfors(content);
                            for (int i = 0; i < sploglist.size(); i++) {
                                SpLogView splog = new SpLogView();
                                splog = sploglist.get(i);
//                                aaa.add(item.getPropertyAsString(0));
                                Map<String, Object> map = new HashMap<String, Object>();
                                map.put("index", (i + 1));
                                map.put("cvouchid", "单据编号：" + splog.getCvouchid());
                                map.put("cdept", "操作时间：" + Basenvo.customDateFormat("yyyy-MM-dd HH:mm:ss", splog.getCdate()));
                                map.put("cnocheckreason", splog.getCmemo());
                                map.put("ddate", "");
                                //更多
                                map.put("data5", "" + splog.getCmemo());
                                map.put("data6", "");
                                map.put("data7", "");
                                map.put("data8", "");
                                dataListForInit.add(map);
                            }
                        } else {
                            AbToastUtil.showToast(Basenvo_splogview.this, R.string.sqlerrormsg);
                        }
                        toolDataProvider.initData(dataListForInit);
                        loadList();
                    }

                    // 失败，调用
                    @Override
                    public void onFailure(int statusCode, String content,
                                          Throwable error) {
                        AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
                        AbDialogUtil.removeDialog(Basenvo_splogview.this);
                    }

                    // 开始执行前
                    @Override
                    public void onStart() {
                        //显示进度框
                        AbDialogUtil.showProgressDialog(Basenvo_splogview.this, 0, "正在加载...");
                    }

                    // 完成后调用，失败，成功
                    @Override
                    public void onFinish() {
                        //移除进度框
                        AbDialogUtil.removeDialog(Basenvo_splogview.this);
                    }
                });
            }

            @SuppressWarnings("unchecked")
            @Override
            public Integer getObject() {
                return -1;
            }
        });
        task.execute(item);
    }
}
